﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;

namespace PlatformClient
{
    public partial class CompanyForm : DevExpress.XtraEditors.XtraForm
    {
        public CompanyForm()
        {
            InitializeComponent();
            BindGrid();
        }

        private void BindGrid()
        {
            try
            {
                DataSet ds = MainForm.PositionService.CompanyGetList("");
                if (ds!=null&& ds.Tables.Count!=0)
                {
                    gridControl1.DataSource = ds.Tables[0];
                    gridView1.GroupPanelText = "共有"+
                        ds.Tables[0].Rows.Count+"条数据";
                }
                
                DataRow row = this.gridView1.GetDataRow(gridView1.FocusedRowHandle);
                if (row != null)
                {
                    label_id.Text = row["Id"].ToString();
                    textEdit_name.Text = row["CompanyName"].ToString();
                    textEdit_remark.Text = row["Remark"].ToString();
                }
            }
            catch (Exception ex)
            {
                Common.CommonRoutine.HandlingExceptions(ex);
                return;
            }


        }

        private void simpleButton_add_Click(object sender, EventArgs e)
        {
            if (btn_add.Tag.ToString() == "新增")
            {
                gridControl1.Enabled = false;
                btn_add.Image = Properties.Resources.UserForm_btnsave001;
                btn_add.Tag = "保存";
                btn_cancel.Location = btn_modify.Location;
                btn_modify.Visible = false;
                btn_cancel.Visible = true;
                btn_delete.Visible = false;
                textEdit_name.Enabled = true;
                textEdit_remark.Enabled = true;             
                label_id.Text = "";
            }
            else
            {
                if (!CheckedPass())
                    return;
                string strName = textEdit_name.Text.ToString();
                string strRemark = textEdit_remark.Text.ToString();
                CompanyInfo companyInfo = new CompanyInfo 
                {Name = strName, Remark = strRemark};
                try
                {
                    if (MainForm.PositionService.CompanyExists(strName))
                    {
                        XtraMessageBox.Show("该公司已存在，请重新命名！");
                        return;
                    }
                    if (MainForm.PositionService.CompanyAdd(companyInfo))
                    {
                        XtraMessageBox.Show("添加成功！");
                        //DataRow row = gridView1.GetDataRow(gridView1.DataRowCount - 1);
                        //label_id.Text = row["id"].ToString();
                         
                        DataSet ds = MainForm.PositionService.CompanyGetList("");
                        gridControl1.DataSource = ds.Tables[0];
                        gridView1.FocusedRowHandle = gridView1.RowCount - 1;
                        DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);
                        if (row != null)
                        {
                            label_id.Text = row["Id"].ToString();
                        }

                    }
                    else
                    {
                        XtraMessageBox.Show("添加失败！");

                    }

                    ResetControl();

                }
                catch (Exception ex)
                {
                    Common.CommonRoutine.HandlingExceptions(ex, true);
                    return;
                }
            }
        }
        private void ResetControl()
        {
            gridControl1.Enabled = true;
            textEdit_name.Enabled = false;
            textEdit_remark.Enabled = false;
            btn_add.Tag = "新增";
            btn_add.Image = Properties.Resources.UserForm_btnadd001;
            btn_add.Visible = true;
            btn_modify.Visible = true;
            btn_cancel.Visible = false;
            btn_delete.Visible = true;
            btn_modify.Image = Properties.Resources.UserForm_btnmodify001;
            btn_modify.Tag = "修改";
            btn_modify.Location = btn_cancel.Location;

            DataRow row = this.gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (row != null)
            {
                label_id.Text = row["id"].ToString();
                textEdit_name.Text = row["CompanyName"].ToString();
                textEdit_remark.Text = row["Remark"].ToString();
            }

        }
        private bool CheckedPass()
        {
            string strName = textEdit_name.Text;
            if (strName.Trim() == "")
            {
                XtraMessageBox.Show("公司名称不能为空！");
                textEdit_name.Focus();
                return false;
            }
            if (textEdit_name.Text.Length > 30)
            {
                XtraMessageBox.Show("分组名称字数过多，请不要超过30个字符!");
                textEdit_name.Focus();
                return false;
            }
           
            return true;
        }

        private void simpleButton_delete_Click(object sender, EventArgs e)
        {
            DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (row != null)
            {
                int id = int.Parse(row["id"].ToString());
                string name = row["CompanyName"].ToString();
                if (XtraMessageBox.Show("确认删除名称为【" + name + "】的公司吗？",
                    "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (MainForm.PositionService.CompanyDelete(id))
                    {
                        XtraMessageBox.Show("删除成功！");
                        textEdit_name.Text = "";
                        textEdit_remark.Text = "";
                        label_id.Text = "";
                        BindGrid();
                    }
                    else
                        XtraMessageBox.Show("删除失败！");
                }
            }
            else
                XtraMessageBox.Show("请选择想要删除的一行数据！");
        }

        private string _companyName;
        private void simpleButton_modify_Click(object sender, EventArgs e)
        {

            if (label_id.Text == "")
            {
                XtraMessageBox.Show("请选择想要修改的公司");
                return;
            }
            if (btn_modify.Tag.ToString() == "修改")
            {
                _companyName = textEdit_name.Text;
                btn_modify.Image = Properties.Resources.UserForm_btnsave001;
                btn_modify.Tag = "保存";
                btn_add.Visible = false;
                btn_cancel.Location = btn_modify.Location;
                btn_modify.Location = btn_add.Location;
                btn_delete.Visible = false;
                btn_cancel.Visible = true;
                textEdit_name.Enabled = true;
                textEdit_remark.Enabled = true;
                gridControl1.Enabled = false;
            }
            else
            {
                if (label_id.Text == "流水号")
                {
                    XtraMessageBox.Show("请选择想要修改的公司");
                    ResetControl();
                    return;
                }
                if (!CheckedPass())
                    return;
                int intId = int.Parse(label_id.Text);
                string strName = textEdit_name.Text;
                string strRemark = textEdit_remark.Text;
                if (strName != _companyName)
                {
                    if (MainForm.PositionService.CompanyExists(strName))
                    {
                        XtraMessageBox.Show("该公司已存在，请重新命名！");
                        textEdit_name.Focus();
                        return;
                    }
                }
                CompanyInfo model = new CompanyInfo 
                { Id = intId,Name = strName, Remark = strRemark };
                if (MainForm.PositionService.CompanyUpdate(model))
                {
                    XtraMessageBox.Show("修改成功！");
                    label_id.Text = "";
                    textEdit_name.Text = "";
                    textEdit_remark.Text = "";

                }
                else
                    XtraMessageBox.Show("修改失败！");
                ResetControl();
                BindGrid();
            }
        }


        private void simpleButton_cancel_Click(object sender, EventArgs e)
        {
            ResetControl();
        }


        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow row = this.gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (row != null)
            {
                label_id.Text = row["id"].ToString();
                textEdit_name.Text = row["CompanyName"].ToString();
                textEdit_remark.Text = row["Remark"].ToString();
            }
        }




        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;
            Cursor = Cursors.Arrow;
        }
    }
}